<?php
namespace app\helpers;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class AliYunService {
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret) {
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    /**
     * @param $phone
     * @return \AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsResponse
     */
    public  function sms($phone,$templateCode,$templateParam=[]) {
        $client = self::createClient(config('sms.aliyun_key'), config('sms.aliyun_secret'));
        $sendSmsRequest = new SendSmsRequest([
            "signName" => config('sms.aliyun_sign_name'),
            "templateCode" => $templateCode,
            "phoneNumbers" => "$phone",
            "templateParam" => json_encode($templateParam)
        ]);
        $runtime = new RuntimeOptions([]);
        // 复制代码运行请自行打印 API 的返回值
        $client->sendSmsWithOptions($sendSmsRequest, $runtime);
    }
}